package user

import (
	"context"
	"encoding/json"
	"errors"
	"hzjt-oam/admin/internal/svc"
	"hzjt-oam/admin/internal/types"
	"hzjt-oam/rpc/sys/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserStatusLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateUserStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserStatusLogic {
	return &UpdateUserStatusLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateUserStatusLogic) UpdateUserStatus(req *types.UserStatusReq) (resp *types.UserStatusResp, err error) {
	if req.Id > 0 || req.Status > 1 {
		logx.WithContext(l.ctx).Errorf("传入的参数有误:%s", err)
		return nil, errors.New("传入的参数有误")
	}

	userId, err := l.ctx.Value("userId").(json.Number).Int64()
	if err != nil {
		return nil, errors.New("验证token未通过")
	}

	user_one, err := l.svcCtx.UserSrv.GetSysUserById(l.ctx, &pb.GetSysUserByIdReq{
		Id: int64(userId),
	})

	_, err = l.svcCtx.UserSrv.UpdateUserStatus(l.ctx, &pb.UserStatusReq{
		Id:           req.Id,
		Status:       req.Status,
		LastUpdateBy: user_one.SysUser.Name,
	})

	if err != nil {
		logx.WithContext(l.ctx).Errorf("状态更新失败:%s", err)
		return nil, errors.New("状态更新失败")
	}

	return &types.UserStatusResp{Message: "成功"}, err
}
